package com.ciban.smarthome.module.ui;


import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.ciban.smarthome.R;
import com.ciban.smarthome.common.utils.RegexUtils;
import com.ciban.smarthome.common.utils.StringUtils;
import com.ciban.smarthome.module.ui.base.BaseActivity;
import com.ciban.smarthome.module.db.dao.HostDao;
import com.ciban.smarthome.module.entity.Host;

public class HostModifyActivity extends BaseActivity {
	private EditText et_no, et_name, et_ip, et_port;
	private Button bt_cancle, bt_save;
	private TextView tv_title;

	private Host host;
	private boolean isEdit = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_host_modify);

		initData();

		initView();
	}

	private void initData() {
		if (getIntent().hasExtra("host")) {
			host = (Host) getIntent().getExtras().get("host");
			isEdit = true;
		}
	}

	private void initView() {
		tv_title = (TextView) findViewById(R.id.host_modify_title);
		et_no = (EditText) findViewById(R.id.addhost_et_no);
		et_name = (EditText) findViewById(R.id.addhost_et_name);
		et_ip = (EditText) findViewById(R.id.addhost_et_ip);
		et_port = (EditText) findViewById(R.id.addhost_et_port);

		bt_cancle = (Button) findViewById(R.id.addhost_bt_cancle);
		bt_save = (Button) findViewById(R.id.addhost_bt_save);

		if (isEdit) {
			tv_title.setText("修改主机信息");
			et_no.setText(host.getSerial_no() + "");
			et_name.setText(host.getName());
			et_ip.setText(host.getIp());
			et_port.setText(host.getPort() + "");

		}

		setListeners();
	}

	@Override
	protected void lvItemClick(int position) {
		super.lvItemClick(position);
	}

	private void setListeners() {
		bt_cancle.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		bt_save.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				saveInfo();
			}
		});
	}

	private void saveInfo() {
		if (host == null)
			host = new Host();
		String ip = et_ip.getText().toString().trim();
		String port = et_port.getText().toString().trim();
		String no = et_no.getText().toString().trim();
		String name = et_name.getText().toString().trim();

		if (StringUtils.isEmpty(ip) || !RegexUtils.isIpNumStr(ip)) {
			displayShort("ip输入错误！");
			return;
		}
		if (StringUtils.isEmpty(port)) {
			displayShort("端口号不能为空！");
			return;
		}
		if (StringUtils.isEmpty(no)) {
			displayShort("编号不能为空！");
			return;
		}
		if (StringUtils.isEmpty(name)) {
			displayShort("名称不能为空！");
			return;
		}

		host.setIp(ip);
		host.setPort(Integer.parseInt(port));
		host.setSerial_no(Integer.parseInt(no));
		host.setName(name);

		HostDao dao = new HostDao(this);

		if (isEdit) {
			int num = dao.updateHost(host);
			if (num >= 0) {
				setResult(RESULT_OK);
				finish();
			} else {
				displayShort("操作失败");
			}
		} else {
			long res = dao.addNewMain(host);
			if (res >= 0) {
				setResult(RESULT_OK);
				finish();
			} else {
				displayShort("操作失败");
			}
		}
	}
}
